package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.example.myapplication.model.ResultOV
import com.example.myapplication.model.Student
import com.example.myapplication.service.StudentService
import com.example.myapplication.utils.HttpUtil
import com.example.myapplication.utils.ServerCreator
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_register.*
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
class RegisterActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_register)
        val activitys=this
        submit02.setOnClickListener(){
            val intentOk1: Intent = Intent(this, MainActivity::class.java)
            val intentError1: Intent = Intent(this, RegisterActivity::class.java)
            if(et_password1.text.toString().equals(et_password2.text.toString())){
                val requestData1 = HashMap<String, String>()
                requestData1.put("username", et_username02.text.toString())
                requestData1.put("password", et_password1.text.toString())
                // sendRequestWithHttpURLConnection(data)
                //  sendRequestWithOkHttp("http://106.15.192.167:8080/userLogin","post",requestData)
                HttpUtil.sendOkHttpRequest("http://10.17.218.37:9100/StudentSystem/registerUser",requestData1,
                    object:Callback{
                        override fun onFailure(call: Call, e: IOException) {
                            Log.e("RegisterActivity","发生错误了")
                            e.printStackTrace();
                        }

                        override fun onResponse(call: Call, response: Response) {
                            val resultData=response.body?.string()
                            var fromgson=Gson().fromJson<ResultOV>(resultData,ResultOV::class.java)
                            if(fromgson.result=="ok"){
                                startActivity(intentOk1)

                            }else{
                                startActivity(intentError1)
                            }
                        }
                    })

            }else{
                Toast.makeText(this,"两次密码不一致,请重新输入",Toast.LENGTH_LONG).show()
            }

        }}}
